package main

import (
	"encoding/gob"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	"gitlab.com/go-box/pongo2gin"
	"log"
	"net/http"
	"os"
	"yak.admin/routers"
	"yak.admin/src/entity"
	"yak.admin/src/middleware"
	_ "yak.admin/src/module/backend/admin/crontab"
	"yak.admin/src/util"
)

func main() {

	gob.Register(entity.Admin{})

	router := gin.Default()
	router.StaticFS("/static", http.Dir("./storage/static"))
	router.StaticFS("/discuss", http.Dir("./storage/discuss"))
	router.StaticFS("/avatar", http.Dir("./storage/avatar"))
	router.StaticFS("/annexes", http.Dir("./storage/annexes"))

	// 创建基于cookie的存储引擎，secret11111 参数是用于加密的密钥
	store := cookie.NewStore([]byte("!@#22321"))
	// 设置session中间件，参数mysession，指的是session的名字，也是cookie的名字
	// store是前面创建的存储引擎，我们可以替换成其他存储引擎
	router.Use(sessions.Sessions("mysession", store))
	// 注册翻译器
	router.Use(middleware.TranslationMiddleware())
	router.Use(middleware.ResponseMiddle())
	router.Use(middleware.Cors())
	// router.Use(middleware.RecoveryMiddleware())

	util.ConfigInit("conf.ini")
	success := util.GetMysqlPool().InitDataPool()
	if !success {
		log.Println("init database pool failure...")
		os.Exit(1)
	}

	// if you need to use custom RenderOptions.
	router.HTMLRender = pongo2gin.New(pongo2gin.RenderOptions{
		TemplateDir: "resources/templates",
		ContentType: "text/html; charset=utf-8",
	})

	r := routers.AdminRouter(router)
	r = routers.ApiRouter(router)

	go r.RunTLS(":9001", "ssl.pem", "ssl.key")
	entity.InitDb()

	_ = r.Run(":9000")
}
